package com.market.service.impl;

import com.market.dto.OrderDetailDto;
import com.market.entity.Goods;
import com.market.entity.Order;
import com.market.entity.OrderDetail;
import com.market.mapper.OrderDetailMapper;
import com.market.service.GoodsService;
import com.market.service.OrderDetailService;
import com.market.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderDetailImpl implements OrderDetailService {

    @Resource
    public OrderDetailMapper orderDetailMapper;

    @Resource
    private GoodsService goodsService;

    @Resource
    private OrderService orderService;

    @Override
    public boolean addOrderDetail(OrderDetail orderDetail) {
        int goodsId = orderDetail.getGoodsId();
        Goods goods = goodsService.queryGoodsByGoodsId(goodsId);
        orderDetail.setGoodsPrice(goods.getPrice());
        //修改订单实付金额
        double actualPay = orderDetail.getBuyNum() * orderDetail.getGoodsPrice();
        orderService.updateActualPay(actualPay, orderDetail.getOrderId());
        //修改订单应付金额
        double shouldPay = orderDetail.getSortNum() * orderDetail.getGoodsPrice();
        orderService.updateShouldPayment(shouldPay, orderDetail.getOrderId());
        return orderDetailMapper.addOrderDetail(orderDetail);
    }

    @Override
    public OrderDetail queryOrderDetail(int orderDetailId) {
        OrderDetail orderDetail = orderDetailMapper.queryOrderDetail(orderDetailId);
        return orderDetail;
    }

    @Override
    public List<OrderDetail> queryOrderDetailByOrderId(int orderId) {
        List<OrderDetail> orderDetails = orderDetailMapper.queryOrderDetailByOrderId(orderId);
        return orderDetails;
    }

    @Override
    public List<OrderDetailDto> queryOrderDetailsByOrderIdDto(int orderId) {
        List<OrderDetail> orderDetails = orderDetailMapper.queryOrderDetailByOrderId(orderId);
        List<OrderDetailDto> orderDetailDto = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetails) {
            OrderDetailDto detailDto = new OrderDetailDto();
            detailDto.setId(orderDetail.getId());
            Goods goods = goodsService.queryGoodsByGoodsId(orderDetail.getGoodsId());
            detailDto.setGoodsName(goods.getGoodsName());
            detailDto.setGoodsPrice(orderDetail.getGoodsPrice());
            detailDto.setBuyNum(orderDetail.getBuyNum());
            detailDto.setSortNum(orderDetail.getSortNum());
            int differNum=Math.abs(orderDetail.getBuyNum()-orderDetail.getSortNum());
            detailDto.setDifferNum(differNum);
            detailDto.setProceeds(detailDto.getBuyNum()*detailDto.getGoodsPrice());
            orderDetailDto.add(detailDto);
        }
        return orderDetailDto;
    }

    @Override
    public int GoodsAllBuyAmount(int goodsId, String payTime) {
        List<OrderDetail> orderDetails = orderDetailMapper.GoodsAllBuyAmount(goodsId, payTime);
        int dayAllNum = 0;
        for (OrderDetail orderDetail : orderDetails) {
            int buyNum = orderDetail.getBuyNum();
            dayAllNum += buyNum;
        }
        return dayAllNum;
    }
}
